﻿using Nutshell.Data;
using Nutshell.Distributing;
using Nutshell.Hardware.OPC;
using Nutshell.Hardware.OPC.Models;
using Nutshell.Messaging.Models;
using Nutshell.NetMQ;

namespace Nutshell.DOS.Business
{
        public class GlobalManager : NotifyPropertyChangedObject
        {
                #region 构造函数

                private GlobalManager()
                {
                }

                #endregion

                #region 单例

                /// <summary>
                ///         单例
                /// </summary>
                public static readonly GlobalManager Instance = new GlobalManager();

                #endregion

                /// <summary>
                ///         日志发送站点
                /// </summary>
                public SendSite<LogMessageModel> LogSite { get; private set; }

                /// <summary>
                ///         语音发送站点
                /// </summary>
                public SendSite<StringValueMessageModel> SynthesizeSite { get; private set; }

                /// <summary>
                ///         OPC服务器连接/断开状态发布站点
                /// </summary>
                public SendSite<BoolValueMessageModel> StatePublishSite { get; private set; }

                /// <summary>
                ///         OPC服务器值更新发布站点
                /// </summary>
                public SendSite<MultiStringKeyStringValueMessageModel> ValuePublishSite { get; private set; }

                /// <summary>
                ///         OPC服务器值查询请求接收站点
                /// </summary>
                public ReceiveSite<MultiStringValueMessageModel> ValueQuerySite { get; private set; }

                /// <summary>
                ///         OPC服务器值写入请求接收站点
                /// </summary>
                public ReceiveSite<MultiStringKeyStringValueMessageModel> ValueWriteSite { get; private set; }


                public AutomationServer OPCServer { get; private set; }


                public void Start()
                {
                        StartLogSite();
                        StartSynthesizeSite();

                        StartStatePublishSite();
                        StartValuePublishSite();

                        LoadOPCServer();


                        StartValueQuerySite();

                        LoadOPCWriteResponseReceiveSite();
                }

                private void StartLogSite()
                {
                        LogSite = NetMQSiteHelper.CreateSendSite<LogMessageModel>(@"配置\Log");
                        LogSite.Start();
                }

                private void StartSynthesizeSite()
                {
                        SynthesizeSite = NetMQSiteHelper.CreateSendSite<StringValueMessageModel>(@"配置\Synthesize");
                        SynthesizeSite.Start();
                }

                private void StartStatePublishSite()
                {
                        StatePublishSite = NetMQSiteHelper.CreateSendSite<BoolValueMessageModel>(@"配置\StatePublish");
                        StatePublishSite.Start();
                }

                private void StartValuePublishSite()
                {
                        ValuePublishSite = NetMQSiteHelper.CreateSendSite<MultiStringKeyStringValueMessageModel>(@"配置\ValuePublish");
                        ValuePublishSite.Start();
                }

                private void StartValueQuerySite()
                {
                        ValueQuerySite = NetMQSiteHelper.CreateReceiveSite<MultiStringValueMessageModel>(@"配置\ValueQuery");
                        ValueQuerySite.Start();
                }

                private void LoadOPCWriteResponseReceiveSite()
                {
                        ValueWriteSite = NetMQSiteHelper.CreateReceiveSite<MultiStringKeyStringValueMessageModel>(@"配置\ValueWrite");
                }


                private void LoadOPCServer()
                {
                        var model = XMLDB.Load<AutomationServerModel>(@"配置\OPCServer.config");

                        OPCServer = new AutomationServer();
                        OPCServer.Load(model);

                        //Server.StartSuccessed += (o, args) =>
                        //{
                        //        //if (SynthesizeServicePublisher == null)
                        //        //{
                        //        //        return;
                        //        //}

                        //        //var message = new StringMessage(Server.Id + "启动成功");
                        //        //var messageModel = new ProtobufStringMessageModel();
                        //        //message.Save(messageModel);

                        //        //SynthesizeServicePublisher.Acquire(messageModel);
                        //};

                        //Server.Start();
                }

                private void LoadPublisher()
                {
                        //foreach (AutomationItem automationItem in Server.AutomationItems)
                        //{
                        //        AutomationItem item = automationItem;
                        //        item.ValueChanged += (o, args) =>
                        //        {
                        //                //var message = new ValueMessage(Server.Id + "启动成功");
                        //                //var messageModel = new ProtobufValueMessageModel();
                        //                //message.Save(messageModel);

                        //                //var response = new ProtobufValueMessageModel
                        //                //{
                        //                //        Id = item.Id,
                        //                //        TypeCode = item.TypeCode,
                        //                //        Value = (item.Value == null)?string.Empty:item.Value.ToString()
                        //                //};
                        //                //Publisher.Acquire(response);
                        //        };
                        //}
                }

                private void LoadSubscriber()
                {
                        //var db = new XmlDB("订阅配置", @"配置\Subscriber.config");
                        //var model = db.Load<XmlMessageSocketModel>();

                        //Receiver = new MessageSubscriber<ProtobufValueMessageModel>();
                        //Receiver.Load(model);

                        //foreach (var item in Server.AutomationItems)
                        //{
                        //        item.ValueChanged += (o, args) =>
                        //        {
                        //                var model = new ProtoAutomationResponseModel()
                        //                {
                        //                        Id = (o as AutomationItem).Id,
                        //                        Value = args.NewValue
                        //                };
                        //                Publisher.Publish(model);
                        //        };
                        //}
                }

                public void Stop()
                {
                        LogSite.Stop();
                }
        }
}